<?php
namespace Home\Controller;

use Home\Util\lib\YZTokenClient;
use Util\AccessController;

/*
 * 有赞接口
 */

class ERPAPIController extends AccessController {

    public function index() {
        if($_GET['access_token']){
            $this->S('access_token',$_GET['access_token']);
        }
        if($_GET['mallid']){
            $this->S('mallid',$_GET['mallid']);
        }
        if($_GET['mallname']){
            $this->S('mallname',$_GET['mallname']);
        }
        $LS = Service('ERPAPI','kx_erp_order');
        $data= $LS->query(array('dr_state'=>0));
        $this->DATA = $data;
        $this->display('Orders');
    }

    /*
     * 导入中间库
     *
     */
    public function LeadIn() {
        $LS = Service('ERPAPI', 'kx_erp_order');
        $res = $LS->GetData(array('dr_state' => 0), $this->S('access_token'));
        if($res['code'] == 'ok'){
            $this->success('导入成功，成功数据：'.
                $res['SUCCESS_NUM'].'条，失败数据：'.
                $res['ERROR_NUM']
            );
        }else{
            $this->error('导入失败，成功数据：'
                .$res['SUCCESS_NUM'].'条，失败数据：'.
                $res['ERROR_NUM']
            );
        }
    }

    /*
     * 获取中间库订单
     */
    public function ZJKOrder(){
        if($_GET['wyy_djbh']){
            $wyy_djbh = $_GET['wyy_djbh'];
        }else{
            $wyy_djbh ='---------------------';
        }
        $LS = Service('ERPAPI', 'kx_erp_order');
        $DATA = $LS->GetzjkOrders($wyy_djbh);
        $this->DATA = $DATA;
        $this->wyy_djbh = $_GET['wyy_djbh'];
        $this->display('ZJKOrders');
    }

    /*
     * 修改订单药店内码
     */
    public function UpdateOrdersYDCode(){
        $danwbh = $_GET['danwbh'];
        $_GET['danwbh'] ?1:$this->error('内码不能为空');
        $wyy_djbh  =$_GET['wyy_djbh'];
        $conn = mssql_conn();
        $sql = "update wyy_Orders_wmx set danwbh='".$danwbh."' where wyy_djbh='".$wyy_djbh."'";
        $sql = iconv('utf-8', 'GB2312//IGNORE', $sql);
        $res = sqlsrv_query( $conn, $sql );
        $LS = Service('ERPAPI', 'yd_code');
        $OK = $LS->update(array('ERP_CODE'=>$_GET['old_danwbh']),array('ERP_CODE'=>$danwbh));
        if($OK){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    /*
     * 修改订单商品内码
     */
    public function UpdateOrdersGOODSCode(){
        $_GET['spbh'] ?1:$this->error('内码不能为空');
        $spbh = $_GET['spbh'];
        $wyy_djbh  =$_GET['wyy_djbh'];
        $conn = mssql_conn();
        $sql = "update wyy_OrderDetails_wmx set spbh='".$spbh
            ."' where wyy_djbh='".$wyy_djbh."' and spbh='".$_GET['old_spbh']."'";
        $sql = iconv('utf-8', 'GB2312//IGNORE', $sql);
        $res = sqlsrv_query( $conn, $sql );
        $LS = Service('ERPAPI', 'goods_code');
        $OK = $LS->update(array('ERP_CODE'=>$_GET['old_spbh']),array('ERP_CODE'=>$spbh));
        if($OK){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

}